package com.ruoyi.web.controller.oa;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 文件管理Controller
 * 
 * @author luguanqing
 * @date 2020-09-03
 */
@Api(value="oa文件管理",tags = "文件管理接口")
@RestController
@RequestMapping("/oa/file")
public class OaFileController extends BaseController
{


    /**
     * 文件上传
     */
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, false);
            if(StringUtils.isNotEmpty(fileName)){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                return ajax;
            }
        }
            return AjaxResult.error("上传文件异常，请联系管理员");

    }

    /**
     * 文件上传
     */
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadOrignal")
    public AjaxResult uploadOrignal(MultipartFile file, @RequestParam("filename")String filename, @RequestParam("relativePath")String relativePath) throws IOException
    {
        if (!file.isEmpty())
        {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getOrginalPath() + "/" + StringUtils.substringBeforeLast(relativePath, "/"), file, true);
            if(StringUtils.isNotEmpty(fileName)){
                AjaxResult ajax = AjaxResult.success("上传成功", fileName);
                return ajax;
            }
        }
        return AjaxResult.error("上传文件异常，请联系管理员");

    }

    /**
     * 文件上传
     */
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadEnclosure")
    public AjaxResult uploadEnclosure(@RequestParam("files")MultipartFile[] files) throws IOException
    {
        if (null != files && files.length > 0)
        {
            List<HashMap<String, String>> fileList = new ArrayList<HashMap<String, String>>();
            for(MultipartFile file : files){
                String fileName = FileUploadUtils.upload(RuoYiConfig.getProfile(), file, false);
                if(StringUtils.isNotEmpty(fileName)){
                    HashMap<String,String> fileMap = new HashMap<String, String>();
                    fileMap.put("fileName", file.getOriginalFilename());
                    fileMap.put("fileUrl", fileName);
                    fileMap.put("fileSize", String.valueOf(file.getSize()));
                    fileList.add(fileMap);
                }
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileList);
            return ajax;
        }
        return AjaxResult.error("上传异常");
    }


}
